package agh.tk.markuptranslation;


public class Text extends AbstractComponent
{
	private TextFormatting textFormatting ;
	private String value ;
	public Text(IComponent left, IComponent right, TextFormatting textFormatting)
	{
		super(left, right) ;
		this.textFormatting = textFormatting ;
	}

	public Text(String value, TextFormatting textFormatting)
	{
		this(null, null, textFormatting) ;
		this.value = value ;
	}
	
	@Override
	public String toString() {
		return value;
	}
	
	@Override
	public String toDokuWiki()
	{
		StringBuilder stringBuilder ;
		if(leftChild != null)
		{
			stringBuilder = new StringBuilder(super.leftChild.toDokuWiki()) ;
			if(rightChild != null)
				stringBuilder.append(super.rightChild.toDokuWiki()) ;
		}
		else
			stringBuilder = new StringBuilder(value) ;
		if(textFormatting == null)
			return stringBuilder.toString() ;
			switch(textFormatting)
			{
				case BOLD :
					stringBuilder.insert(0, "**") ;
					stringBuilder.append("**") ;
					break ;
				case DELETED :
					stringBuilder.insert(0, "<del>") ;
					stringBuilder.append("</del>") ;
					break ;
				case ITALIC :	
					stringBuilder.insert(0, "//") ;
					stringBuilder.append("//") ;
					break ;
				case SUBSCRIPT :
					stringBuilder.insert(0, "<sub>") ;
					stringBuilder.append("</sub>") ;
					break ;
				case SUPERSCRIPT :
					stringBuilder.insert(0, "<sup>") ;
					stringBuilder.append("</sup>") ;
					break ;
				case UNDERLINE :
					stringBuilder.insert(0, "__") ;
					stringBuilder.append("__") ;
					break ;
				case FOOTNOTE :
					stringBuilder.insert(0, "((") ;
					stringBuilder.append("))") ;
					break ;
				case MONOSPACED :
					stringBuilder.insert(0, "<code>") ;
					stringBuilder.append("</code>") ;
					break ;
				case VERBATIMLINE :
					stringBuilder.insert(0, "\n  ") ;
					stringBuilder.append("\n") ;
					break ;
			}
		return stringBuilder.toString() ;
	}
	
	@Override
	public String toMediaWiki() {
		StringBuilder stringBuilder ;
		if(leftChild != null)
		{
			stringBuilder = new StringBuilder(super.leftChild.toMediaWiki()) ;
			if(rightChild != null)
				stringBuilder.append(super.rightChild.toMediaWiki()) ;
		}
		else
			stringBuilder = new StringBuilder(value) ;
		if(textFormatting == null)
			return stringBuilder.toString() ;
		switch(textFormatting)
			{
				case BOLD :
					stringBuilder.insert(0, "'''") ;
					stringBuilder.append("'''") ;
					break ;
				case DELETED :
					stringBuilder.insert(0, "<del>") ;
					stringBuilder.append("</del>") ;
					break ;
				case ITALIC :	
					stringBuilder.insert(0, "''") ;
					stringBuilder.append("''") ;
					break ;
				case SUBSCRIPT :
					stringBuilder.insert(0, "<sub>") ;
					stringBuilder.append("</sub>") ;
					break ;
				case SUPERSCRIPT :
					stringBuilder.insert(0, "<sup>") ;
					stringBuilder.append("</sup>") ;
					break ;
				case UNDERLINE :
					stringBuilder.insert(0, "<u>") ;
					stringBuilder.append("</u>") ;
					break ;
				/*case FOOTNOTE :
					stringBuilder.insert(0, "((") ;
					stringBuilder.append("))") ;
					break ;*/
				case MONOSPACED :
					stringBuilder.insert(0, "<code>") ;
					stringBuilder.append("</code>") ;
					break ;
				case VERBATIMLINE :
					stringBuilder.insert(0, "\n  ") ;
					break ;
			}
		return stringBuilder.toString() ;
	}
	
	@Override
	public String toHTML() {
		StringBuilder stringBuilder ;
		if(leftChild != null)
		{
			stringBuilder = new StringBuilder(super.leftChild.toHTML()) ;
			if(rightChild != null)
				stringBuilder.append(super.rightChild.toHTML()) ;
		}
		else
			stringBuilder = new StringBuilder(value) ;
		if(textFormatting == null)
			return stringBuilder.toString() ;
		switch(textFormatting)
			{
				case BOLD :
					stringBuilder.insert(0, "<b>") ;
					stringBuilder.append("</b>") ;
					break ;
				case DELETED :
					stringBuilder.insert(0, "<del>") ;
					stringBuilder.append("</del>") ;
					break ;
				case ITALIC :	
					stringBuilder.insert(0, "<i>") ;
					stringBuilder.append("</i>") ;
					break ;
				case SUBSCRIPT :
					stringBuilder.insert(0, "<sub>") ;
					stringBuilder.append("</sub>") ;
					break ;
				case SUPERSCRIPT :
					stringBuilder.insert(0, "<sup>") ;
					stringBuilder.append("</sup>") ;
					break ;
				case UNDERLINE :
					stringBuilder.insert(0, "<u>") ;
					stringBuilder.append("</u>") ;
					break ;
				/*case FOOTNOTE :
					stringBuilder.insert(0, "((") ;
					stringBuilder.append("))") ;
					break ;*/
				case MONOSPACED :
					stringBuilder.insert(0, "<code>") ;
					stringBuilder.append("</code>") ;
					break ;
				case VERBATIMLINE :
					stringBuilder.insert(0, "\n  ") ;
					break ;
			}
		return stringBuilder.toString() ;
	}
	
	@Override
	public String toTxt2tags() 
	{
		StringBuilder stringBuilder ;
		if(leftChild != null)
		{
			stringBuilder = new StringBuilder(super.leftChild.toTxt2tags()) ;
			if(rightChild != null)
				stringBuilder.append(super.rightChild.toTxt2tags()) ;
		}
		else
			stringBuilder = new StringBuilder(value) ;
		if(textFormatting == null)
			return stringBuilder.toString() ;
		switch(textFormatting)
			{
				case COMMENT :
					stringBuilder.insert(0, "\n%") ;
					stringBuilder.append("\n") ;
					break ;
				case DELETED :
					stringBuilder.insert(0, "--") ;
					stringBuilder.append("--") ;
					break ;
				case UNDERLINE :
					stringBuilder.insert(0, "__") ;
					stringBuilder.append("__") ;
					break ;
				case BOLD :
					stringBuilder.insert(0, "**") ;
					stringBuilder.append("**") ;
					break ;
				case ITALIC :
					stringBuilder.insert(0, "//") ;
					stringBuilder.append("//") ;
					break ;
				case MONOSPACED :
					stringBuilder.insert(0, "``") ;
					stringBuilder.append("``") ;
					break ;
				case VERBATIMLINE :
					stringBuilder.insert(0, "\n```") ;
					stringBuilder.append('\n') ;
					break ;
			}
		
		return stringBuilder.toString() ;
	}

	public void setTextFormatting(TextFormatting textFormatting) {
		this.textFormatting = textFormatting;
	}

}
